import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:my_fluter_basic/pages/home/widgets/home_like.dart';
import 'package:my_fluter_basic/pages/home/widgets/home_nav.dart';
import 'package:my_fluter_basic/pages/home/widgets/home_search.dart';
import 'package:my_fluter_basic/pages/home/widgets/home_swipe.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final Dio _dio = Dio(
    BaseOptions(
      baseUrl: 'http://smart-shop.itheima.net/index.php?s=/api/',
    ),
  );

  List _swipeList = [];
  List _navList = [];
  List _goodsList = [];

  _getData() async {
    Response res = await _dio.get('page/detail');
    List items = res.data['data']['pageData']['items'];
    setState(() {
      _swipeList = items[1]['data'];
      _navList = items[3]['data'];
      _goodsList = items[6]['data'];
    });
  }

  @override
  void initState() {
    _getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          '智慧商城',
          style: TextStyle(
            fontSize: 18,
            color: Colors.white,
          ),
        ),
        backgroundColor: Colors.red,
      ),
      body: ListView(
        children: [
          const HomeSearch(),
          HomeSwipe(swipeList: _swipeList),
          HomeNav(navList: _navList),
          Image.network(
            'https://smart-shop.itheima.net/uploads/10001/20230320/647c6bde4b1f1a3e88955e5f596344f9.png',
          ),
          Container(
            height: 44,
            color: Colors.white,
            alignment: Alignment.center,
            child: const Text(
              '- 猜你喜欢 -',
            ),
          ),
          HomeLike(goodsList: _goodsList),
        ],
      ),
    );
  }
}
